package com.lwf.component.spider.iteye.essence;

import com.lwf.core.processor.AbstractPageDetailsProcessor;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.selector.Html;

import java.util.List;

@Component
public class IteyeEssenceProcessor extends AbstractPageDetailsProcessor {

    Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    protected List<String> getListUrlRegex() {
        return Lists.newArrayList("^http://www\\.iteye\\.com/magazines\\?page=[0-9]+");
    }

    protected List<String> getDetailsUrlRegex() {
        return Lists.newArrayList("http://www\\.iteye\\.com/magazines/.+");
    }

    protected List<String> getDetailsUrlList(Html html) {
        List<String> urlList = html.css("html body div#page div#content.clearfix div#main div#index_main div.news.clearfix div.content h3 a")
                .links().all();
        return urlList;
    }


    protected List<String> getNextPageUrl(Html html) {
        String url = html.css("html body div#page div#content.clearfix div#main div#index_main div.pagination a.next_page").links().get();
        return Lists.newArrayList(url);
    }

    protected List<String> getTitle(Html html) {
        String title = html.xpath("/html/body/div/div[2]/div[1]/div[3]/div[1]/h1/a/text()").get();
        return Lists.newArrayList(title);
    }

    @Override
    protected String getHtmlStr(Html html) {
        String s = html.get();
        return s;
    }

    public Site getSite() {
        return site;
    }
}
